package test.com.sms.selenium.singlepoint.util;

import org.openqa.selenium.By;

import test.com.sms.selenium.framework.SeleniumPageUtil;
import test.com.sms.selenium.util.SeleniumUtil;

public class SpHRSkillUserListPageUtil extends SeleniumPageUtil{

	public final static By SMUL_SPAN_MAIN_HEADER = By.id("ctl44_hdrSkillManagement_lblHeader");
	public final static By SMUL_LINK_USER_LIST = By.xpath("//*[@id='ecbbb4c2-3348-4be6-9842-2c61474a7b0d']/a");
	
	public final static By SMUL_SPAN_USER_LIST_HEADER = By.id("ctl44_UserListReport_Header_lblHeader");
	
	public final static By SMUL_IMG_REPORT_PREFERENCES = By.id("ctl44_UserListReport_Header_imgPreferences");
	public final static By SMUL_IMG_EXPORT = By.id("ctl44_UserListReport_Header_imgExcelExport");
	public final static By SMUL_IMG_REFRESH = By.id("ctl44_UserListReport_Header_imgRefresh");
	
	public final static By SMUL_TD_PAGE = By.xpath("//*[@id='dvUserList']/table[2]/tbody/tr/td[1]");
	public final static By SMUL_SPAN_PAGE_START_NO = By.id("ctl44_UserListReport_PagingTop_lblPagingPageNumber");
	public final static By SMUL_TD_OF = By.xpath("//*[@id='dvUserList']/table[2]/tbody/tr/td[3]");
	public final static By SMUL_SPAN_PAGE_COUNT = By.id("ctl44_UserListReport_PagingTop_lblPagingPageCount");
	
	public final static By SMUL_INPUT_FIRST = By.id("ctl44_UserListReport_PagingTop_imgFirst");
	public final static By SMUL_INPUT_PREVIOUS = By.id("ctl44_UserListReport_PagingTop_imgPrevious");
	public final static By SMUL_INPUT_NEXT = By.id("ctl44_UserListReport_PagingTop_imgNext");
	public final static By SMUL_INPUT_LAST = By.id("ctl44_UserListReport_PagingTop_imgLast");
	
	public final static By SMUL_IMG_FILTER_1 = By.xpath("//*[@id='ctl44_UserListReport_ReportOutput']/table/tbody/tr[1]/th[1]/img");
	public final static By SMUL_FILTER_INPUT = By.id("ctl44_UserListReport_txtFilterInput");
	public final static By SMUL_FILTER_BTN_ENTER = By.id("ctl44_UserListReport_btnFilterEnter");
	public final static By SMUL_FILTER_BTN_CANCEL = By.id("ctl44_UserListReport_btnFilterCancel");
	public final static By SMUL_IMG_FILTER_2 = By.xpath("//*[@id='ctl44_UserListReport_ReportOutput']/table/tbody/tr[1]/th[2]/img");
	public final static By SMUL_TH_USER_NAME = By.xpath("//*[@id='ctl44_UserListReport_ReportOutput']/table/tbody/tr[2]/th[1]");
	public final static By SMUL_TH_SKILL_COUNT = By.xpath("//*[@id='ctl44_UserListReport_ReportOutput']/table/tbody/tr[2]/th[2]");
	public final static By SMUL_SPAN_RESULT = By.id("ctl44_UserListReport_ReportOutput");
	
	public static void enterHRSkillManagementUserListPage() {
		SpHomePageUtil.enter();
		SpHomePageUtil.enterHRSkillManagementPage();
		SeleniumUtil.click(SeleniumUtil.getCachedElement(SMUL_LINK_USER_LIST));
		waitForPageToLoad();
		waitForResultRefresh();
	}
	public static void enterHRSkillManagementUserList() {
		SpHomePageUtil.enterHRSkillManagementPage();
		SeleniumUtil.click(SeleniumUtil.getCachedElement(SMUL_LINK_USER_LIST));
		waitForPageToLoad();
	}
	
	public static void waitForPageToLoad() {
		SeleniumUtil.selectDefaultContent();
		SeleniumUtil.selectFrame(SpHomePageUtil.HOME_FRAME_CONTENT_NAME);
		SeleniumUtil.waitOnElements(new By[]{
				SMUL_IMG_EXPORT, SMUL_INPUT_FIRST, SMUL_INPUT_PREVIOUS, 
				SMUL_INPUT_NEXT, SMUL_INPUT_LAST, SMUL_IMG_FILTER_1, SMUL_IMG_FILTER_2
			});
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SMUL_IMG_REPORT_PREFERENCES));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SMUL_IMG_EXPORT));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SMUL_INPUT_FIRST));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SMUL_INPUT_PREVIOUS));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SMUL_INPUT_NEXT));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SMUL_INPUT_LAST));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SMUL_IMG_FILTER_1));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SMUL_IMG_FILTER_2));
	}
	
	/**wait for result refresh, if time out 30 seconds not response return an exception*/
	public static void waitForResultRefresh(){
		long beforeWait = System.currentTimeMillis();
		long afterWait;
		boolean refreshEnd = SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(SMUL_IMG_REFRESH));
		
		while(refreshEnd){
			afterWait = System.currentTimeMillis();
			if((afterWait - beforeWait) > 30 * 1000){
				SeleniumUtil.fail("timeout 30 seconds not response");
				return;
			}else{
				SpHRSkillUserListPageUtil.waitForPageToLoad();
				refreshEnd = SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(SMUL_IMG_REFRESH));
			}
		}
	}
}
